<template>
  <el-dialog title="编辑部门" :visible.sync="isShow" width="500">
    <el-form label-width="100px" :model="form" :rules="rules" ref="form">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="部门编辑" prop="code">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select @focus="focusEvent" v-model="form.manager">
          <el-option
            v-for="i in userList"
            :key="i.id"
            :label="i.username"
            :value="i.username"
            >{{ i.username }}</el-option
          >
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input type="textarea" v-model="form.introduce"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button @click="isShow = false">取消</el-button>
        <el-button @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
// 获取简单的员工列表
import { sysUserSimple } from '@/api/departments.js'
export default {
  name: 'DialogItem',
  //   接收原始数据
  props: {
    innitList: {
      // 类型是一个数组
      Type: Array,
      //   默认是一个数组
      default: () => []
    }
  },
  data () {
    return {
      isShow: false,
      //   简单的员工数据
      userList: '',
      form: {
        name: '', //	string	非必须		部门编码，同级部门不可重复
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              // 找出同级
              const temp = this.innitList.filter(item => {
                return item.pid === this.item.id
              })
              console.log('找同级', temp)
              //   部门之间不能重复
              const result = temp.some(item => {
                return item.name === value.trim()
              })
              result
                ? callback(new Error(`请不要输入相同的名字:` + value))
                : callback()
            },
            trigger: 'blur'
          }
        ],

        code: [{ required: true, message: '必填', trigger: 'blur' }],
        manager: [{ required: true, message: '必填', trigger: 'blur' }],
        introduce: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      item: ''
    }
  },
  mounted () {
    this.$bus.$on('show', (bol, item) => {
      this.isShow = bol
      this.item = item
      console.log('ghjkl', item.id)
      //   console.log('fghjk', this.item)
    })
  },
  methods: {
    async focusEvent () {
      //   console.log(dfghjk)
      if (this.userList.length === 0) {
        const res = await sysUserSimple()
        console.log('简单的员数据', res)
        this.userList = res.data
      }
    },
    submit () {
      this.$refs.form.validate(result => {
        if (result) {
          this.$message.success('验证成功')
        }
      })
    }
  }
}
</script>

<style></style>
